D:\a\tools.proto\tools.proto\compiler\src\gen\swift\message\decl.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::message::Message; |
30 | | use crate::compiler::Protocol; |
31 | | use crate::gen::base::map::TypePathMapper; |
32 | | use crate::gen::base::message::{gen_msg_field_decl, generate, Templates}; |
33 | | use crate::gen::base::Error; |
34 | | use crate::gen::codec::CodecMap; |
35 | | use crate::gen::swift::util::{SwiftTypeMapper, SwiftUtils}; |
36 | | use crate::gen::template::Template; |
37 | | use itertools::Itertools; |
38 | | |
39 | | const TEMPLATE: &[u8] = include_bytes!("decl.template"); |
40 | | const TEMPLATE_EXT: &[u8] = include_bytes!("ext.template"); |
41 | | |
42 | 0 | fn gen_initializer( |
43 | 0 | templates: &Templates, |
44 | 0 | msg: &Message, |
45 | 0 | type_path_map: &TypePathMapper<SwiftTypeMapper>, |
46 | 0 | ) -> Result<String, Error> { |
47 | 0 | let init_field_list = msg |
48 | 0 | .fields |
49 | 0 | .iter() |
50 | 0 | .map(|field| gen_msg_field_decl::<SwiftUtils, _>(field, templates, type_path_map)) |
51 | 0 | .collect::<Result<Vec<String>, Error>>()? |
52 | 0 | .into_iter() |
53 | 0 | .map(|v| v[..v.len() - 1].to_string()) |
54 | 0 | .join(", "); |
55 | 0 | let initializers = msg |
56 | 0 | .fields |
57 | 0 | .iter() |
58 | 0 | .map(|field| templates.template.scope().var("name", &field.name).render("decl", &["initializer"]).unwrap()) |
59 | 0 | .join(""); |
60 | 0 | Ok(templates |
61 | 0 | .template |
62 | 0 | .scope() |
63 | 0 | .var("init_field_list", init_field_list) |
64 | 0 | .var("initializers", initializers) |
65 | 0 | .render("", &["decl"]) |
66 | 0 | .unwrap()) |
67 | 0 | } |
68 | | |
69 | 0 | pub fn gen_message_decl(proto: &Protocol, codec_map: &CodecMap, msg: &Message) -> Result<String, Error> { |
70 | 0 | let type_path_map = TypePathMapper::new(&proto.type_path_map, SwiftTypeMapper::from_protocol(proto)); |
71 | 0 | let mut templates = Templates { |
72 | 0 | template: Template::compile(TEMPLATE_EXT).unwrap(), |
73 | 0 | codec_map, |
74 | 0 | }; |
75 | 0 | templates.template.var("proto_name", proto.name()).var("msg_name", &msg.name); |
76 | 0 | let initializer = gen_initializer(&templates, msg, &type_path_map)?; |
77 | 0 | templates.template = Template::compile(TEMPLATE).unwrap(); |
78 | 0 | templates.template.var("proto_name", proto.name()).var("initializer", initializer); |
79 | 0 | generate::<SwiftUtils, _>(templates, msg, &type_path_map) |
80 | 0 | } |